package com.earstep.view
{
	import com.erastep.effect.spark.viewStackEffects.Cube;
	import com.erastep.mvc.EraStepFrame;
	import com.erastep.mvc.ifc.IFrame;
	import com.erastep.mvc.ifc.INotification;
	import com.erastep.mvc.mediator.Mediator;
	import com.erastep.mvc.module.vo.ModuleVO;
	import com.erastep.mvc.view.HomeGroup;
	import com.erastep.util.CommonConstants;
	import com.erastep.util.GUID;
	import com.erastep.util.LogUtil;
	import com.erastep.util.MessageConstants;
	import com.erastep.util.StringUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.clearInterval;
	import flash.utils.clearTimeout;
	import flash.utils.getDefinitionByName;
	import flash.utils.setTimeout;
	
	import mx.containers.Canvas;
	import mx.containers.ViewStack;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.events.CloseEvent;
	
	import spark.components.Application;
	import spark.components.Group;
	import spark.components.Image;
	import spark.effects.Animate;
	
	/**
	 * @depart 宁波维涛
	 * @author Jarvis 
	 * @E-mail: shaomj@victopsoft.com
	 * @version 1.0.1 
	 * 创建时间：2011-10-11 上午09:05:44 * 
	 */
	public class MainViewMediator extends Mediator
	{
		
		public static const NAME:String = "victopMainView";
		
		public function MainViewMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			mgr = viewComponent as Application;
			registerMediator(new CommonNotificationsMediator(mgr));
		}
		
		override public function onRegister():void{
			(mgr["downIco"] as Image).addEventListener(MouseEvent.CLICK,showAllHomeList);
			(mgr["upIco"] as Image).addEventListener(MouseEvent.CLICK,hiddenAllHomeList);
			(mgr["quit"] as Label).addEventListener(MouseEvent.CLICK,quitSystem);
		}
		
		/**
		 * 显示门户集合 
		 * */
		private function showAllHomeList(e:MouseEvent):void{
			(mgr["downIco"] as Image).visible = false;
			(mgr["upIco"] as Image).visible = true;
			mgr["hltlist"].top = 0;
		}
		/**
		 * 隐藏门户数据
		 * */
		private function hiddenAllHomeList(e:MouseEvent):void{
			(mgr["downIco"] as Image).visible = true;
			(mgr["upIco"] as Image).visible = false;
			 mgr["hltlist"].top = -29;
		}
		
		private var loader:Loader;
		
		private var context:LoaderContext;
		
		private var mgr:Application;
		
		private var first:Boolean = false;
		
		
		private var loginOut:Boolean = false;
		
		private var effect:Cube;
		
		public function startKeepLive1(obj:Object):void{
			//存储向后台发送的请求
			effect = new Cube();
			effect.duration = 1200;
		}
		/**
		 * 退出系统
		 * */
		private function quitSystem(e:MouseEvent):void{
			Alert.yesLabel = "是";
			Alert.noLabel = "否";
			Alert.show("您确定要退出系统么?","系统提示",1|2,null,closeWindow);
		}
		
		/**
		 * 关闭窗口
		 * */
		private function closeWindow(e:CloseEvent):void{
			if(e.detail == Alert.YES){
				loginOut = true;
				sendNotification("loginOutSystem",null,null,CommonConstants.VICTOPFRAME);
				sendNotification("logoutSystem");
			}
		}
		
		public function respondToLoadPluginAndResource(note:INotification):void{
			var param:Object = note.getBody().item;
			LogUtil.print("开始查找模版!");
			var tempvo:ModuleVO = note.getBody().modulevo as ModuleVO;
			//统一参数名称
			param.systemId = param.BZsystemid;
			param.formId = param.formid;
			
			//开始加载插件
			var iframe:IFrame = EraStepFrame.getInstance();
			
			//如果没有状态,默认状态为1
			if(!param.hasOwnProperty(CommonConstants.OPER_TYPE)){
				param[CommonConstants.OPER_TYPE]  = MessageConstants.TYPE_1;
			}
			if(!iframe.hasLoadedModule(tempvo.resourceid)){
				loader=new Loader();
				context=new LoaderContext(false, ApplicationDomain.currentDomain);
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void{
					LogUtil.print("ID:" + tempvo.resourceid + "   swfUrl:" + tempvo.url + " Load complete!");
					var cls:Class = getDefinitionByName("com.victop."+tempvo.resourceid+".Main") as Class;
					// at here register module
					iframe.registerModule(tempvo.resourceid, cls);
					LogUtil.print("$$%%$$"+StringUtil.objectToJson(note.getBody()));
					sendNotification(tempvo.resourceid,note.getBody(),param.key,CommonConstants.MODULE);		
				});
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void{
					LogUtil.print("########Load Fail:"+StringUtil.objectToJson(tempvo));
					//sendNotification(tempvo.resourceid,param,key,CommonConstants.MODULE);	
				});
				
				//LogUtil.print("插件加载路径:"+host+"/"+tempvo.url);
				loader.load(new URLRequest(tempvo.url), context);
				//监听进度条					
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress); 
			}else{
				sendNotification(tempvo.resourceid,note.getBody(),param.key,CommonConstants.MODULE);	
			}
			
		}
		/**
		 * 显示门户登陆
		 * */
		public function respondToShowLoginPage(note:INotification):void{
			(mgr["loginStart"] as Group).visible = false;
			(mgr["mainView"] as ViewStack).addChild(note.getBody() as DisplayObject);
		}
		/**
		 * 显示插件
		 * */
		public function respondToNewWindow(note:INotification):void{
			LogUtil.print("开始呈现画面!!!!!");
			var obj:Object = note.getBody();
			if(obj.windowId){
				var mid:String;
				if(obj.hasOwnProperty("windowId")){
					mid = obj.windowId;
				}else{
					mid = GUID.create();
				}
				obj.windowId = mid;
				obj.fromId = note.getFromId();
				sendNotification("showViewInHomePage",obj,null,CommonConstants.HOMEPAGE);	
			}
		}
		
		/**
		 * 显示已经登录门户主页
		 * */
		public function respondToShowHomePage(note:INotification):void{
			var m:ViewStack = mgr["mainView"] as ViewStack;
			if(note.getBody() is HomeGroup){
				var flg:Boolean = false;
				if(first){
					for(var i:uint= 0;i < m.numChildren;i++){
						if(m.getChildAt(i) is Canvas){
							if((m.getChildAt(i) as Canvas).name == note.getFromId()){
								flg = true;
								break;
							}
						}
					}
				}
				if(!flg){
					if(!first){
						mgr["hltlist"].visible = true;
						mgr["hltlist"].top = -29;
						(mgr["loginStart"] as Group).visible = false;
						(m.getChildAt(0) as Canvas).setStyle("showEffect", effect);
						(m.getChildAt(0) as Canvas).setStyle("hideEffect", effect);
					}
					mgr.mouseChildren = false;
					mgr.mouseEnabled = false;
					var hg:HomeGroup = note.getBody() as HomeGroup;
					hg.id = note.getFromId();
					var carvas:Canvas = new Canvas();
					carvas.setStyle("showEffect", effect);
					carvas.setStyle("hideEffect", effect);
					carvas.percentHeight = 100;
					carvas.percentWidth = 100;
					carvas.name = note.getFromId();
					carvas.addChild(hg);
					m.addChild(carvas);
					var cl:uint = setTimeout(function():void{
						m.selectedIndex = m.getChildIndex(carvas);
						clearTimeout(cl);
					},500);
					first = true;
				}else{
					LogUtil.print("不支持单插件多门户!");
					Alert.show("不支持单插件多门户!");
				}
			}else{
				LogUtil.print("请使用门户组件!");
				Alert.show("门户组件引用!");
			}
		}
		/**
		 * 显示未登录的门户
		 * */
		public function respondToShowNoLoginHomePage(note:INotification):void{
			var m:ViewStack = mgr["mainView"] as ViewStack;
			if(note.getBody() is HomeGroup){
				var flg:Boolean = false;
				if(first){
					for(var i:uint= 0;i < m.numChildren;i++){
						if(m.getChildAt(i) is Canvas){
							if((m.getChildAt(i) as Canvas).name == note.getFromId()){
								flg = true;
								break;
							}
						}
					}
				}
				if(!flg){
					if(!first){
						mgr["hltlist"].visible = true;
						mgr["hltlist"].top = -29;
						(mgr["loginStart"] as Group).visible = false;
						(m.getChildAt(0) as Canvas).setStyle("showEffect", effect);
						(m.getChildAt(0) as Canvas).setStyle("hideEffect", effect);
					}
					mgr["hltlist"].visible = false;
					//mgr.mouseChildren = false;
					//mgr.mouseEnabled = false;
					var hg:HomeGroup = note.getBody() as HomeGroup;
					hg.id = note.getFromId();
					var carvas:Canvas = new Canvas();
					carvas.setStyle("showEffect", effect);
					carvas.setStyle("hideEffect", effect);
					carvas.percentHeight = 100;
					carvas.percentWidth = 100;
					carvas.name = note.getFromId();
					carvas.addChild(hg);
					m.addChild(carvas);
					var cl:uint = setTimeout(function():void{
						m.selectedIndex = m.getChildIndex(carvas);
						clearTimeout(cl);
					},500);
					first = true;
				}else{
					LogUtil.print("不支持单插件多门户!");
					Alert.show("不支持单插件多门户!");
				}
			}else{
				LogUtil.print("请使用门户组件!");
				Alert.show("门户组件引用!");
			}
		}

	
		
		/**
		 * 成功退出系统
		 * */
		public function respondToLogoutSuccess(note:INotification):void{
			ExternalInterface.call("function(){window.location.href = window.location.href}");
		}
		/**
		 * 退出系统
		 * */
		public function loginOutSystem(e:CloseEvent):void{
			respondToLogoutSuccess(null);
		}
		/**
		 * 系统退出
		 * */
		public function respondToLoginOutSystem(note:INotification):void{
			(mgr["loginStart"] as Group).visible = true;
			(mgr["loginText"] as Label).text = "正在注销...";
			(mgr["loginOut"] as Animate) .play();
		}
		
		/**
		 * 加载插件 进度条
		 * */
		private function handleProgress(e:ProgressEvent):void{
			trace("插件=========已加载:"+e.bytesLoaded+" 文件大小:"+e.bytesTotal);
		}
		
		
	}
}